/**
 * Created by ercha on 2016/1/1.
 */

$(document).ready(function(){

    //1. 设置 input 的 type="file"，如果要上传多个，加上 multiple
    //initFileInput("avatar", "/upload");

    var avatar = "";

    //发送完的回调函数
    $('#avatar').on('fileuploaded', function(event, data, previewId, index, reader){
        avatar = data.response.url;
        submitForm(avatar);
    });

    $('#updateForm').validate({
        rules:{
            avatar: {
                //required: true
            },
            sex:{
                //required:true
            },
           tel: {
                //required: true,
               number: true
            },
           address: {
               //required: true
            }
        },
        //error 是一个 <label> 块，label 的内容是错误信息
        errorPlacement: errorPlacement,
        success: success,
        submitHandler: function(form){
            waitMessage();
            if($(".file-input-ajax-new").length>0 || $(".file-preview-thumbnails").attr("id")!=undefined){
                console.info("no file input!");
                submitForm(null);
            } else{
                $('#avatar').fileinput('upload');
            }
        }
    });
});

function submitForm(avatar) {
    $.ajax({
        url: path + "/profile/update",
        type: "POST",
        dataType: "json",
        data: {
            avatar: avatar,
            sex: getRadio('sex'),
            tel: $('#tel').val(),
            address: $('#address').val()
        },
        success: function (data) {
            if (data.success == true) {
                successMessage("操作成功！", function(){
                    location.reload();
                });
            } else {
                errorMessage("操作失败！");
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            errorMessage("操作失败！错误代码：" + XMLHttpRequest.status);
        }
    });
}/*
 * Created by yx on 2016/1/3.
 */
